<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-header">
        <div class="logo-container">
          <i class="el-icon-shopping-cart-2 logo-icon"></i>
        </div>
        <h1>水果超市管理系统</h1>
        <p class="subtitle">欢迎使用，请登录您的账号</p>
      </div>
      
      <div class="login-form-container">
        <el-form :model="rulesForm" ref="loginForm">
          <el-form-item prop="username">
            <el-input 
              v-model="rulesForm.username" 
              placeholder="请输入用户名" 
              prefix-icon="el-icon-user">
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input 
              v-model="rulesForm.password" 
              placeholder="请输入密码" 
              prefix-icon="el-icon-lock" 
              type="password">
            </el-input>
          </el-form-item>
          
          <el-form-item prop="role">
            <el-select 
              v-model="rulesForm.role" 
              placeholder="请选择角色">
              <el-option
                v-for="item in menus"
                v-if="item.hasBackLogin=='是'"
                :key="item.roleName"
                :label="item.roleName"
                :value="item.roleName">
              </el-option>
            </el-select>
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              class="login-button" 
              @click="login">
              登录系统
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <div class="login-footer">
        <p>© 2025 水果超市管理系统</p>
      </div>
    </div>
  </div>
</template>

<script>
import menu from "@/utils/menu";

export default {
  data() {
    return {
      rulesForm: {
        username: "",
        password: "",
        role: "",
      },
      menus: [],
      tableName: ""
    };
  },
  mounted() {
    this.menus = menu.list();
  },
  methods: {
    login() {
      if (!this.rulesForm.username) {
        this.$message.error("请输入用户名");
        return;
      }
      if (!this.rulesForm.password) {
        this.$message.error("请输入密码");
        return;
      }
      if (!this.rulesForm.role) {
        this.$message.error("请选择角色");
        return;
      }
      
      let menus = this.menus;
      for (let i = 0; i < menus.length; i++) {
        if (menus[i].roleName == this.rulesForm.role) {
          this.tableName = menus[i].tableName;
        }
      }
      
      this.$http({
        url: `${this.tableName}/login?username=${this.rulesForm.username}&password=${this.rulesForm.password}`,
        method: "post"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$storage.set("Token", data.token);
          this.$storage.set("role", this.rulesForm.role);
          this.$storage.set("sessionTable", this.tableName);
          this.$storage.set("adminName", this.rulesForm.username);
          this.$router.replace({ path: "/index/" });
        } else {
          this.$message.error(data.msg);
        }
      });
    }
  }
};
</script>

<style lang="scss">
.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: url(../assets/img/bg.jpg) no-repeat center center;
  background-size: cover;
  position: relative;
  
  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.8);
  }
}

.login-container {
  width: 420px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 50px rgba(0, 0, 0, 0.1), 0 6px 20px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  position: relative;
  z-index: 1;
  transform: translateY(0);
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 60px rgba(0, 0, 0, 0.12), 0 8px 25px rgba(0, 0, 0, 0.08);
  }
}

.login-header {
  padding: 30px 0 20px;
  text-align: center;
  background: linear-gradient(135deg, #4CAF50, #8BC34A);
  
  .logo-container {
    width: 70px;
    height: 70px;
    margin: 0 auto 15px;
    background: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    
    .logo-icon {
      font-size: 36px;
      color: #4CAF50;
    }
  }
  
  h1 {
    margin: 0;
    color: white;
    font-size: 24px;
    font-weight: 500;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  }
  
  .subtitle {
    color: rgba(255, 255, 255, 0.9);
    font-size: 14px;
    margin: 8px 0 0;
  }
}

.login-form-container {
  padding: 35px 40px 25px;
  
  .el-form-item {
    margin-bottom: 25px;
    
    &:last-child {
      margin-bottom: 0;
    }
  }
  
  .el-input {
    /deep/ .el-input__inner {
      height: 46px;
      border-radius: 8px;
      border: 1px solid #e0e0e0;
      padding-left: 45px;
      transition: all 0.3s ease;
      
      &:hover {
        border-color: #d0d0d0;
      }
      
      &:focus {
        border-color: #4CAF50;
        box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.1);
      }
    }
    
    /deep/ .el-input__prefix {
      left: 15px;
      color: #909399;
    }
  }
  
  .el-select {
    width: 100%;
    
    /deep/ .el-input__inner {
      padding-left: 15px;
    }
  }
  
  .login-button {
    width: 100%;
    height: 46px;
    background: linear-gradient(135deg, #4CAF50, #8BC34A);
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    letter-spacing: 1px;
    transition: all 0.3s ease;
    
    &:hover, &:focus {
      background: linear-gradient(135deg, #43A047, #7CB342);
      border: none;
      transform: translateY(-2px);
      box-shadow: 0 7px 14px rgba(76, 175, 80, 0.2);
    }
    
    &:active {
      transform: translateY(0);
    }
  }
}

.login-footer {
  padding: 15px 0;
  text-align: center;
  border-top: 1px solid #f0f0f0;
  
  p {
    margin: 0;
    color: #909399;
    font-size: 12px;
  }
}
</style>
